﻿using SCM.Common;
using SCM.Common.Extensions;
using SCM.Model;
using SCM.Model.DBEntity;
using SCM.Servie.IBusiness;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace SCM.Servie.Business
{
    /// <summary>
    /// 规则项服务
    /// </summary>
    public class RuleItemService : IRuleItemService
    {
        private readonly DbContext dal = new DbContext();

        public string Add(T_RuleItem model)
        {
            return dal.Insertable(model).ExecuteReturnEntity().ID;
        }

        public bool Delete(List<string> ids)
        {
            return dal.Deleteable<T_RuleItem>().Where(p => ids.Contains(p.ID)).ExecuteCommand() > 0;
        }

        public T_RuleItem GetById(string id)
        {
            return dal.Get<T_RuleItem>(id);
        }

        public List<T_RuleItem> GetList(string ruleID)
        {
            Expression<Func<T_RuleItem, bool>> whereExp = (a) => true;
            if (!string.IsNullOrWhiteSpace(ruleID))
            {
                whereExp = whereExp.Compose((a) => a.RuleID == ruleID, Expression.AndAlso);
            }
            return dal.Queryable<T_RuleItem>().Where(whereExp).ToList();
        }

        public PageData<T_RuleItem> Query(string ruleID, string casNo, string ecNO, string cName, string eName, PageInfo page)
        {
            int count = 0;
            Expression<Func<T_RuleItem, bool>> whereExp = (a) => true;
            if (!string.IsNullOrWhiteSpace(ruleID))
            {
                whereExp = whereExp.Compose((a) => a.RuleID == ruleID, Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(casNo))
            {
                whereExp = whereExp.Compose((a) => a.CASNo.StartsWith(casNo), Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(ecNO))
            {
                whereExp = whereExp.Compose((a) => a.ECNo.StartsWith(ecNO), Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(cName))
            {
                whereExp = whereExp.Compose((a) => a.CName.StartsWith(cName), Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(eName))
            {
                whereExp = whereExp.Compose((a) => a.Ename.StartsWith(eName), Expression.AndAlso);
            }

            List<T_RuleItem> list = dal.Queryable<T_RuleItem>().Where(whereExp).OrderBy(a => a.CASNo, SqlSugar.OrderByType.Asc).ToPageList(page.PageIndex, page.PageSize, ref count);
            page.DataCount = count;
            return new PageData<T_RuleItem> { Data = list, Page = page };
        }

        public bool SaveOrUpdate(string ruleId, List<T_RuleItem> list)
        {
            try
            {
                dal.BeginTran();
                dal.Deleteable<T_RuleItem>().Where(p => p.RuleID == ruleId).ExecuteCommand();
               
                bool result = dal.Insert<T_RuleItem>(list);
                if (result == false)
                {
                    dal.RollbackTran();
                    return false;
                }

                dal.CommitTran();
                return true;
            }
            catch (Exception ex)
            {
                dal.RollbackTran();
                throw ex;
            }
        }

        public bool Update(T_RuleItem model)
        {
            return dal.Modify(model);
        }
    }
}
